class ExpensesController < ApplicationController
  # GET /expenses
  # GET /expenses.xml
  
  before_filter :login_required
  before_filter :access_control, :only => [:edit, :update, :destroy]
  #before_filter :has_permission?

  def index
    @user = current_user
  	@group = @user.group
  	@group_users = @group.users.select {|x| x != @user}
	
  	@recently_added_group = @user.find_expenses(5)
	
  	@total_positive = @user.calculate_positive
  	@total_negative = @user.calculate_negative
  end
  
  # GET /expenses/1
  # GET /expenses/1.xml
  def show
    @user = current_user
    @expense = Expense.find(params[:id].to_i, :include => :user)

  	@pay_name = "Group Split"
  	pay_id = @expense.pay_id
  	#Turn into CASE later
  	if(pay_id == Expense.paytypes[:MULT_SPLIT])
  		@pay_name = "Multiple Split"
  	elsif(pay_id == Expense.paytypes[:OWES_ME])
  		@pay_name = "Owe" #Change to AMBER owes CYNTHIA
  	end
  end
  
  # GET /expenses/new
  # GET /expenses/new.xml
  def new 
    @expense = Expense.new
  	form

  end

  # GET /expenses/1/edit
  def edit   
    @expense = Expense.find(params[:id].to_i, :include => [:group, :user])
  	form	
  end

  # POST /expenses
  # POST /expenses.xml
  def create 
    @expense = Expense.new(params[:expense])
    @expense.user = current_user
    @expense.group = current_user.group
    
    if @expense.save
	    @expense.make_individuals
      flash[:notice] = 'Expense was successfully created.'
      redirect_to :controller=>"expenses"
    else
      form
  		render :action => "new"
    end

  end
  
  # PUT /expenses/1
  # PUT /expenses/1.xml
  def update
    @expense = Expense.find(params[:id].to_i, :include => [:user, :group, :individuals])

    if @expense.update_attributes(params[:expense])
  		for indv in @expense.individuals
  			indv.destroy
  		end
	    @expense.make_individuals
      flash[:notice] = 'Expense was successfully updated.'
      redirect_to :controller=>"expenses"
    else
	    form
	    render :action => "edit"
    end
  end

  # DELETE /expenses/1
  # DELETE /expenses/1.xml
  def destroy
    @expense = Expense.find(params[:id].to_i)   
  	@expense.destroy
  	redirect_to :controller=>"expenses"
  end
  
  #Action for marking individuals as paid
  def mark_paid
    #params[:id] if the individual id of the individual to consolidate
	  Individual.find(params[:id].to_i).markaspaid
  	flash[:notice] = 'Expense was successfully marked paid.'
  	redirect_to :controller=>"expenses"
  	
  	#FUTURE - Add Ajax
  end
  
  #Action for consolidating expenses
  def consolidate
    #params[:id] is the user id of the user to consolidate with
    Individual.consolidate(current_user, User.find(params[:id].to_i))	
  	flash[:notice] = 'Successfully consolidated.'
  	redirect_to :controller=>"expenses"
  	
  	#FUTURE - Add Ajx
	
  end
  
  def list_indv
	  type = params[:id].to_i
  	page = params[:page]
  	@type = type
  	if(type.to_s == "1") #I owe
  		#@list = Individual.paginate_where_payer(current_user.id, page)
  		@list = Individual.find(:all, :conditions=>{:payer => current_user.id, :con => false}, :include=>:expense)
   	else #(type == "2") #People owe me
  		@list = Individual.find(:all, :conditions=>{:payee => current_user.id, :con => false}, :include=>:expense)
  	end
  end
  
  def list_expense
	  page = params[:page]
  	#@list = Expense.paginate_expenses(current_user.group_id, page)
  	@list = Group.find(current_user.group_id, :include=>[{:expenses => :user}]).expenses
  	@user = current_user
  end
  
  #Action to mark payment from someone else
  def markpayment
	  other = User.find(params[:person])
  	amount = params[:amount]
  	
  	if(is_a_number?(amount))
  	  payment = Payment.new(:payer => other.id, :payee => current_user.id, :amount => amount, :group_id => current_user.group_id)
  	  payment.save
  	  
  	  current_user.mark_payment_from(other, amount)
  	  flash[:notice] = 'Successfully marked payment from ' + other.name
	  else
	    flash[:error] = 'Please enter in a valid dollar amount.'
    end
  	redirect_to :controller=>"expenses"
  end

  def list_payment
    page = params[:page]
    @user = current_user
    @list = Payment.find(:all, :conditions=>["payee = ? OR payer = ?", @user.id, @user.id])
  end
  
  private
  def form
    @user = current_user
	  @pay_types = { "Owes Me" => Expense.paytypes[:OWES_ME], "Multiple Split" => Expense.paytypes[:MULT_SPLIT], "Group Split" => Expense.paytypes[:GROUP_SPLIT]}
	  @group_users = @user.group.users.select {|x| x != @user}
  end
  
  def is_a_number?(s)
    s.to_s.match(/\A[+-]?\d+?(\.\d+)?\Z/) == nil ? false : true 
  end
  
  def access_control
    if (Expense.find(params[:id].to_i).user != current_user)
      flash[:error] = "You are not allowed to edit others' expenses."
      redirect_to '/home'
    end
  end
  

end
